# 0 leds
# 1 switches (interrupt at bit 11)
# 2 seven seg hex
#
# leds turn on with the switches
# seven segs count the switch transitions

### KERNEL ###

addi $t0, $0, 0xC      # set isr address
mtc0 $t0, $15          # set ebase

j start                # go to user code

isr: mfc0 $k0, $13     # load cause register

andi $k1, $k0, 0x0800  # mask switch interrupt
bne $k1, $0, isr_switches # if switches, go to handler

eret                   # else return, no handler for that interrupt
# Note: It would be possible here to add some code that
# deactivates unhandled interrupts in the status register
# but it should be set in the init code below

isr_switches:          # specific isr for the switches
lw $k1, ($s1)          # get switches
sw $k1, ($s0)          # put to leds
addi $s6, $s6, 1       # increment switch counter
sw $s6, 4($s2)         # write the count register to the seven segs
eret                   # return to interrupted code

### USER ###

start:                 # begin initialisation

# set up interrupt mask
ori $t0, $0, 0x0801    # we only want switches, ie bit 11, and have interrupts on, bit 0
mtc0 $t0, $12          # set status word in cp0

# set up MMIO offsets
lui $s0, 0xFFFF        # leds
ori $s1, $s0, 0x0040   # switches
ori $s2, $s0, 0x0080   # seven seg displays

addi $t0, $0, 0x000F   # word to turn all seven segs on
lui $s6, 0x00F0        # set mask for write enables and no dots, also use as counter
sw $t0, 0($s2)         # turn all seven segs on
sw $s6, 4($s2)         # write zeros without dots

loop: j loop
